package com.atguigu.gmall.cart.filter;

import com.atguigu.gmall.cart.util.UserThreadLocalUtils;
import com.atguigu.gmall.model.user.UserInfo;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@Order(1)
//@WebFilter(filterName = "appTokenFilter", urlPatterns = "/*")
public class CartFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        // 测试和开发环境不过滤
        String userId = request.getHeader("userId");
        if(userId!=null){
            UserInfo userInfo = new UserInfo();
            userInfo.setId(Long.parseLong(userId));
            UserThreadLocalUtils.setUser(userInfo);
        }
        chain.doFilter(req,res);
    }
}